package com.yswl.yeeduo.ui.task;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.yswl.yeeduo.App;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.asynctask.BaseHttpTask;
import com.yswl.yeeduo.ui.bean.ActiviteDetailBean;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.http.HttpUtils;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.UrlParamsConfig;

public class ActivityListTask extends BaseHttpTask<Map<String, String>, Void, Object> {
	public static int mTotalPage;
	public ActivityListTask(Context context,int taskId, TaskDelegate delegate) {
		super(context,taskId, delegate);
	}

	/**
	 * 首先执行该方法，在真正的后台操作前北UI线程调用，可在该方法内执行一些准备工作，如：进度条或控件的实例化
	 */

	protected void onPreExecute() {
		super.onPreExecute();
	}
	
	@Override
	protected Object doInBackground(Map<String, String>... params) {
		
		String str = "";
		String url = UrlParamsConfig.getURLEncapsulation(Constant.ConfigNet.get_activity_list, params[0], ServiceAuthManager.getSecret(context));
		try {
			str = HttpUtils.sendGetMethod4Path(url, new HashMap<String, Object>(), "utf-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return new NetworkException();
		} catch (IOException e) {
			e.printStackTrace();
			return new NetworkException();
		}
		
		return str;
	}


	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		
		if (result instanceof NetworkException) {
			this.getDelegate().faild_TaskDelegate(super.getTaskId(), "网络异常");
			return;
		}
		
		
		JSONObject json = null;
		try {
			json = new JSONObject(result+"");
			if (super.isSuccess(json)) {
				JSONObject jsonObj = json.getJSONObject(DATA);
				mTotalPage = jsonObj.getInt("total");
				List<ActiviteDetailBean> listdata = ActiviteDetailBean.json2List(jsonObj.getString("list"));
				if (listdata.size()>0 && listdata!= null) {
					this.getDelegate().success_TaskDelegate(super.getTaskId(),listdata);//
				}
			}
			
		} catch (JSONException e) {
			this.getDelegate().faild_TaskDelegate(super.getTaskId(), "数据异常");
		}
	}
	

	/**
	 * 用户取消线程时调用
	 */
	protected void onCancelled() {
		super.onCancelled();
	}
	
	
}
